package 多线程初阶.示例测试;

/**
 * 线程安全示例:多个线程同时修改同一个变量
 */
public class TestDemo13 {
    static class Counter{
        public int count=0;
        void increase(){
            count++;
        }
    }
    public static void main(String[] args) throws InterruptedException {
        final Counter counter=new Counter();
        Thread threadA=new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });
        Thread threadB=new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });
        threadA.start();
        threadB.start();
        threadA.join();
        threadB.join();
        System.out.println("预计值为100000,实际值为"+counter.count);
    }
}
